package mxx.mr.job;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;
import java.math.BigDecimal;

/**
 * @program:report-form-hadoop
 * @description:设备OEE-MR
 * @author:AlTuring
 * @Date:2019/11/3-18:14
 **/
public class DeviceOEEMR {
    public static class DeviceOEEMapper extends Mapper<LongWritable, Text, Text, Text> {
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

            // -- 构造key
            String[] datas = value.toString().split(",");
            //设备编码
            String deviceCode = datas[4];
            //设备名称
            String deviceName = datas[3];
            //设备类型
            String deviceType = datas[6];
            //设备型号
            String deviceModel = datas[5];
            //时间（2019-10-01）
            String time = datas[1].substring(0,10);

            //定义key （设备编码-设备名称-设备类型-设备型号-时间）
            //FIXME 设备的编码+名称+类型+型号 不能确定设备唯一性，只有设备表的ID和唯一编码才是唯一的
            Text outKey = new Text(deviceCode + "_" +
                    deviceName + "_" +
                    deviceType + "_" +
                    deviceModel + "_" +
                    time);
            // -- 写出
            context.write(outKey, value);

        }
    }

    public static class DeviceOEEReducer extends Reducer<Text, Text, Text, Text> {

        @Override
        protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
            //上班时间 (分钟)
            BigDecimal workTime = new BigDecimal("0");
            //待机时间
            BigDecimal standbyTime = new BigDecimal("0");
            //加工时间
            BigDecimal machiningTime = new BigDecimal("0");
            //总零件数
            BigDecimal total = new BigDecimal("0");
            //不合格的工件数量
            BigDecimal failedNum = new BigDecimal("0");

            //FIXME 统计上班时间 (每天8小时上班时间)
            workTime = new BigDecimal(480);
            //统计待机时间和作业时间
            for (Text value: values) {
                String[] datas = value.toString().split(",");
                standbyTime = standbyTime.add(new BigDecimal(datas[0]));
                machiningTime = machiningTime.add(new BigDecimal(datas[2]));
            }
            Text outValue = new Text(key.toString().replace("_", ",") + ","+workTime+ ","+standbyTime+ ","+machiningTime+ ","+total+ ","+failedNum);

            // -- 使用联合key的hash散列作为key
            Text outKey = new Text(DigestUtils.shaHex(key.toString()));

            context.write(outKey, outValue);
        }
    }

    public static void main(String[] args) throws Exception{
        Configuration configuration = new Configuration();
        configuration.set("mapred.textoutputformat.separator", ",");

        // 如果输出目录已经存在，则先删除
        FileSystem fileSystem = FileSystem.get(configuration);

        // 输出文件
        Path outputPath = new Path(args[1]);
        if (fileSystem.exists(outputPath)) {
            fileSystem.delete(outputPath, true);
        }
        // 输入文件
        Path inputPath = new Path(args[0]);

        Job job = Job.getInstance(configuration);
        job.setJarByClass(DeviceOEEMR.class);

        job.setMapperClass(DeviceOEEMapper.class);
        job.setReducerClass(DeviceOEEReducer.class);

        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);

        FileInputFormat.setInputPaths(job, inputPath);
        FileOutputFormat.setOutputPath(job,outputPath);

        job.waitForCompletion(true);

        System.out.println("---设备OEE数据完成---");
    }
}
